<?php

namespace app\model;

//use support\Model;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaSupplierExamineApproveFlow
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_supplier_examine_approve_flow';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition)
    {
        $list = Db::table('wa_supplier_examine_approve_flow')
            ->select('*')
            ->where($condition)
            ->get();

        return json_encode($list);
    }

    public static function postsCount($start_time,$end_time)
    {

        $start_time = date('Y-m-d H:i:s', $start_time);
        $end_time = date('Y-m-d H:i:s', $end_time);
        $provider_count =Db::table('wa_supplier_examine_approve_flow')
            ->select('*')
//            ->where('post_type','product')
            ->whereBetween('created_at',[$start_time,$end_time])
            ->count(); // 用户数量
        return $provider_count;

    }




    /**
     * 销售订单审批流 -列表-分页
     * @param Request $request
     * @return mixed
     */
    public static function list($page, $pageNum, $fields = ['*'], $condition = [], $between = []){

        $offset = ($page - 1) * $pageNum;
        if ($between) {
            $model = Db::table('wa_supplier_examine_approve_flow')
                ->select($fields)
                ->where($condition)
                ->whereBetween('created_at', $between);
        } else {
            $model = Db::table('wa_supplier_examine_approve_flow')
                ->select($fields)
                ->where($condition);
        }

        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();

//        if ($list){
//            foreach ($list as $k){
//
//            }
//        }
        return json_encode(['list' => $list, 'count' => $count]);


    }


    /**
     * 销售订单审流 -创建
     * @param $data
     * @return int
     */
    public static function create($data)
    {
        return Db::table('wa_supplier_examine_approve_flow')->insertGetId($data);
    }


    /**
     * 销售订单审批流-状态修改
     * @param $data
     * @return int
     */
    public static function updated($condition,$data)
    {
        return Db::table('wa_supplier_examine_approve_flow')->where($condition)->update($data);
    }


    /**
     * 销售订单审批流-查询
     * @param $data
     * @return int
     */
    public static function find($condition)
    {
        $res = Db::table('wa_supplier_examine_approve_flow')->select('*')->where($condition)->first();
        return json_encode($res);
    }




    /**
     * @param Request $request
     * @return mixed
     */
    public static function allIn($adminId)
    {
        // $adminId 当前登入用户信息
        // 获取审批流详情中所有 该用户所在部门的信息
        $res = Db::table('wa_supplier_examine_approve_flow')
            ->select('*')
            ->where(['department_id' => intval($adminId->department_id)])
            ->orderBy('id', 'desc')
            ->get();
        $admins = [];
        $ids = [];
        if($res){
            // 每笔审批都会有一条自动创建的 一级部门待审批记录
            // 遍历数组中 admin_ids 中 包含当前用户的数据对应的 订单编号 生成数组
            foreach ($res  as $key){
                if ($key->admin_ids){
                    $admin_ids = json_decode($key->admin_ids);
                    // $needle参数表示需要检测的值，$haystack参数表示要检测的数组，$strict参数是一个可选的参数，表示是否开启严格模式。在默认情况下，$strict参数的值为false，因此可以省略不写。
                    if (in_array($adminId->id,$admin_ids)){
//                    array_push($admins,$key->admin_ids);
                        array_push($admins,$key->sales_order_sn);
                    };
                }

            }
        }

//        $list = Db::table('wa_supplier_examine_approve_flow')
//            ->select('admin_ids')
//            ->whereIn('admin_ids',$admins)
//            ->get();
        $admins = array_unique($admins);
        return json_encode($admins);
    }


}